const regeneratorRuntime = require('../../vendor/regenerator-runtime')
// const cp = require("../common/load-page.js")
const gotop = require("../common/top.js")
const util = require("../../utils/util")
const co = require('../../vendor/co')
// const share = require('../../utils/share.js')
const app = getApp()
const position = {}
const pool = new Map()
let catScrollLeft = 0

Page({
    data: {
        loading: false,
        cid: -1,
        category: [{
                id: -1,
                name: "我的品牌"
            },
            {
                id: 0,
                name: "关注"
            }
        ],
        brand: true,
        showTips: true,
        showCategory: false,
        array: []
    },
    onLoad(options) {
        app.station_id = ''
        // share(options)
        gotop.init()
        wx.getSetting({
            success: res => {
                if (res.authSetting['scope.userInfo']) {
                    getApp().http.get('city/tiny-station/vip-spread', {
                        data: {
                            member_id: options.pmid || wx.getStorageSync('mid'),
                        }
                    }).then(res => {
                        console.log(res, "JOIN resulr*****************")
                    }).catch(err => {})
                }
            }
        })
        // if (wx.getStorageSync('no-tip')) {
        //     this.setData({
        //         showTips: false,
        //     })
        // }

        // 加载分类
        wx.getStorageSync('user') &&app.http.get('city/msg/get-all-wzfl').then(res => {
            if (this.data.brand) {
                this.setData({
                    category: [{
                            id: -1,
                            name: "我的品牌"
                        },
                        {
                            id: 0,
                            name: "关注"
                        }
                    ],
                    category: this.data.category.concat(res.data.brands),
                    choice: res.data.brands
                })
                for (var i = 0; i < res.data.brands.length; i++) {
                    // this.setData({
                    //     [`choice[${i}].check`]: Math.random() > .8
                    // })
                    this.setData({
                        [`choice[${i}].check`]: false
                    })
                }
                if (wx.getStorageSync('user')) {
                    this.setData({
                        brand: res.data.brandFlag
                    })
                }
                this.clickCategory({
                    currentTarget: {
                        dataset: {
                            id: -1
                        }
                    }
                })
                this.loadData()
            }
        })
    },
    noTips() {
        wx.setStorageSync('no-tip', true)
        this.setData({
            showTips: false,
        })
    },
    closeTips() {
        this.setData({
            brand: true
        })
    },
    onShow() {
        // 加载分类
        wx.getStorageSync('user')&&app.http.get('city/msg/get-all-wzfl').then(res => {
            this.setData({
                brand: res.data.brandFlag
            })
        })
        wx.getStorage({
            key: 'store',
            success: function(res) {
                wx.setNavigationBarTitle({
                    title: res.data,
                })
            },
        })
    },
    // 显示选择分类
    getCategory() {
        let choice = this.data.choice
        this.setData({
            showCategory: true,
            choice: choice
        })
    },

    // 选择分类
    choiceCategory(e) {
        var index = e.currentTarget.dataset.index
        var choice = this.data.choice
        var check = choice[index].check;
        choice[index].check = !check
        this.setData({
            choice: choice
        })
    },
    sureCat() {
        console.log('xxxxxxxxxx')
        var arr = this.data.choice.filter(o => {
            return o.check == true
        })
        var array = arr.map(o => {
            return o.id
        })
        this.setData({
            array: array,
            cid: 0,
            showCategory: false,
            tag: {
                left: 10,
                width: 28
            },
        })
        const tab = this.getTab()
        tab.page = 0
        tab.nextFlag = true
        tab.list = []
        tab.loading = false
        this.loadData()
        console.log(arr, array)
    },
    cancelCat() {
        this.setData({
            showCategory: false
        })
    },
    onInput(evt) {
        this.setData({
            content: evt.detail.value
        });
    },
    // 搜索
    doSearch() {
        if (!this.data.content) {
            wx.showToast({
                title: '请输入您要搜索的内容~',
                icon: 'none'
            })
            return
        }
        wx.navigateTo({
            url: './search?content=' + this.data.content,
        })
    },
    // 加载文章列表
    loadData() {
        let tab = this.getTab()
        if (tab.loading || !tab.nextFlag) {
            return
        }
        tab.loading = true
        this.setData({
            loading: true
        })

        const data = {
            kind: this.data.cid,
            type: 0
        }
        if (this.data.cid == 0) {
            // 关注
            data.type = 1,
            data.kind = this.data.array
        }
        if (this.data.cid == -1) {
            // 我的品牌
            data.type = 3
        }
        app.http.get("city/msg/get-all-article?page=" + tab.page + '&kind=' + data.kind + '&type=' + data.type).then(res => {
            tab.loading = false
            tab.page = tab.page + 1
            // if (res.data) {
            tab.list.push(...res.data.articleData.data)
            // }
            for (var i = 0; i < tab.list.length; i++) {
                tab.list[i].images = tab.list[i].msg_content.find(o => {
                    return !!o.images
                })
            }
            tab.nextFlag = res.data.articleData.nextFlag
            this.setData(tab)
            wx.stopPullDownRefresh()
            wx.hideLoading()
        }).catch(err => {
            app.wxToast.error(err.message)
            console.log(err)
        })
    },

    getTab() {
        if (!pool.has(this.data.cid)) {
            pool.set(this.data.cid, {
                position: 0,
                page: 0,
                nextFlag: true,
                list: [],
                loading: false
            })
        }
        return pool.get(this.data.cid)
    },
    //点击一级分类
    clickCategory(evt) {
        if (this.data.cid != evt.currentTarget.dataset.id) {
            this.setData({
                cid: evt.currentTarget.dataset.id,
            })
            const tab = this.getTab()
            this.setData(tab)
            if (tab.list.length == 0) {
                this.loadData()
            }
            if (wx.pageScrollTo) {
                wx.pageScrollTo({
                    scrollTop: tab.position,
                })
            }
            if (wx.createSelectorQuery) {
                this.setData({
                    hasQuery: true
                })
                const query = wx.createSelectorQuery()
                query.select("#cat" + this.data.cid).boundingClientRect()
                query.select("#cat").boundingClientRect()
                query.exec(res => {
                    this.setData({
                        tag: {
                            left: res[0].left + catScrollLeft,
                            width: res[0].width,
                        }
                    })
                })
            }
        }
    },

    onPullDownRefresh: function() {
        const tab = this.getTab()
        tab.page = 0
        tab.nextFlag = true
        tab.list = []
        tab.loading = false
        this.loadData()
    },

    onReachBottom: function() {
        this.loadData()
    },
    onShareAppMessage: function() {
        return app.getShareObj('邀请你来逛逛' + wx.getStorageSync('store'))
    },
    onPageScroll(e) {
        this.getTab().position = e.scrollTop
        gotop.onPageScroll
    },
    onCatScroll(e) {
        catScrollLeft = e.detail.scrollLeft
    }
})